<?php

class M_po extends CI_Model {

	function __construct()
	{
		$this->load->database();
	}
	
	// --------------------------------------------------------------------

      /** 
       * function Produk_Po()       	   
       * @param $form_data - array
	   *
	   * function SimpanProduk_po()       	   
       * @param $form_data - array
	   *
	   * function EditProduk_po()       	   
       * @param $form_data - array
	   *
	   * function HapusProduk_po()       	   
       * @param $form_data - array
	   *
	   * function SimpanProduk()       	   
       * @param $form_data - array
	   *
       * @return Bool - TRUE or FALSE
       */
  
	function f_purchasing($proses,$param,$form_data)
	{
		/*Total Kondisi Where Query*/
		$tot_param 	= sizeof($param);
		$rows_array = 0;
		$kondisi	= "";
		switch ($proses){
		case 1: /*Insert*/
			$petugas 	= $this->session->userdata('username');
			
			$NoPo 		= $form_data['no_po'];
			$PoDate		= $form_data['po_date'];
			$SuppId		= $form_data['kd_supplier'];
			$Tgl_JT		= $form_data['tgl_jt'];
			$sts		= $form_data['status']; /*1-Pesan 2-Penerimaan 3-Lunas 4-Pending*/
			$sql 		= "INSERT INTO PURCHASING (PO_NUMBER,		PO_DATE,	SUPPLIER_ID,	TGL_JT_PEMBAYARAN, 	CREATED_BY,
												   STATUS)
					                	   values ('$NoPo', 		'$PoDate', 	'$SuppId', 		'$Tgl_JT',			'$petugas',
										   		   '$sts');";
			return $this->db->query($sql);											
			
			break;
		case 2: /*Update*/
			break;		
		case 3: /*Delete*/
			break;	
		case 4: /*Select*/
			/*Set Kondisi Where Query*/
			if($tot_param > 0){
				foreach($param as $value)
				{
					/*----------------------
					- Nambah Parametter Kondisi Where Tinggal Di Tambah di Casenya
					------------------------*/
					++$rows_array;
					switch ($rows_array){
					case 1: /*ProKode*/
						if(!empty($value)){
							$kondisi=$kondisi."prokode='".$value."'";
						}
					
					case 2: /*Nama*/
						if(!empty($value)){
							$kondisi=$kondisi." and like ucase(nama)='%".$value."%'";
						}
					
					}
				}
				/*End Statement SQL*/
				$kondisi=$kondisi.";";				
				$sql = "SELECT * FROM PRODUK WHERE ".$kondisi;
			}
			else{
				/* Tanpa Kondisi */
				$sql = "SELECT * FROM PRODUK WHERE NAMA LIKE '"."%'";
			}
			//return $this->db->query($sql)->result();
			$query = $this->db->query($sql);			
			if ($query->num_rows() > 0){
				foreach( $query->result() as $data ){
					$produk[] = $data;
				}
				return $produk;
					
			}
			 
		}
		return FALSE;			
		break;						
	}
	
	function f_purchasing_detail($proses,$param,$form_data)
	{	
		/*Total Kondisi Where Query*/
		$tot_param 	= sizeof($param);
		$rows_array = 0;
		$kondisi	= "";
		
		switch ($proses){
		case 1: /*Insert*/
		
			break;
		case 2: /*Update*/
		
			break;		
		case 3: /*Delete*/
		
			break;	
		case 4: /*Select*/	
		
			break;				
		}
	}
	
	//Generate No Purchasing Order	
	function NoPo()
	{
		$sql     = "select max(id) as maxno from purchasing";
		$row     = $this->db->query($sql)->row();
		$maxno   = $row->maxno;

		$year    = date("y");
		$bln     = date("n");
        $jenis	 = "2"; // Jenis Pembelian
		$nopo    = sprintf("$year$bln$jenis%08d",$maxno + 1);

		/*
		echo "<script type='text/javascript'>alert('$nopo');</script>";
		*/
		return $nopo;
	}	
	
	//Cek Keberadaan No Purchasing Order	
	function CekNoPo($nopo)
	{
		$sql = "select count('$nopo') as ada from purchasing where po_number = '$nopo'";
		$row = $this->db->query($sql)->row();

		return $row->ada;
	
	}
		
	function SaveForm($form_data)
	{
		$this->db->insert('purchasing', $form_data);
		
		if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
}
?>